-- This file controls the updater and the resources.

-- This is NOT automatic because I hate automatic updaters.
-- Really, if I want to update something, I don't want you checking every 30 seconds.
-- (Steam is fine, though)

if !SERVER then return end

resource.AddFile( 'sound/fireworks/firework_explosion_1.wav' )
resource.AddFile( 'sound/fireworks/firework_explosion_2.wav' )
resource.AddFile( 'sound/fireworks/firework_explosion_3.wav' )
resource.AddFile( 'sound/fireworks/firework_launch_1.wav' )
resource.AddFile( 'sound/fireworks/firework_launch_2.wav' )

local FW_Version = 2
local LastCall = 0

local function CheckUpdate( contents, size )

	if !contents or contents == '' then return end
 
	local NewVersion = tonumber( contents ) or 1
	local CurVersion = FW_Version or 1
	
	if NewVersion > CurVersion then
	
		MsgAll( string.format( 'There is an update available for fireworks\nYou have r%s, the current is r%s!\nDownload at: http://dl.dropbox.com/u/8269322/Fireworks/Fireworks.zip', CurVersion, NewVersion ) )

	elseif NewVersion == CurVersion then
	
		MsgAll( 'Your fireworks are up to date' )
	
	else
	
		MsgAll( 'Error in checking fireworks version, please try again.' )
	
	end
	
end

local function CallUpdate( ply, cmd, args )

	if !ply:IsAdmin() then return end
	
	if CurTime() < LastCall + 10 then
		local Seconds = -( CurTime() - LastCall - 10) 
		MsgAll( string.format( 'Please %s more seconds before checking for an update!', math.floor( Seconds ) ) ) -- Preventing people from spamming command and raping dropbox.
		return
	end
	
	MsgAll( 'Checking Fireworks version...' )
	
	if FrameTime() == 0 then
		MsgAll( '(Please Unpause)' ) // http.Get doesn't work while paused.
	end
	
	http.Get( 'http://dl.dropbox.com/u/8269322/Fireworks/v.txt', '', CheckUpdate );

	LastCall = CurTime()
	
end
concommand.Add( 'fireworks_checkupdate', CallUpdate )
